<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="异常对象json格式" prop="exceptionJson">
      <el-input v-model="dataForm.exceptionJson" placeholder="异常对象json格式"></el-input>
    </el-form-item>
    <el-form-item label="异常信息" prop="exceptionMessage">
      <el-input v-model="dataForm.exceptionMessage" placeholder="异常信息"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="ip地址" prop="ip">
      <el-input v-model="dataForm.ip" placeholder="ip地址"></el-input>
    </el-form-item>
    <el-form-item label="ip来源" prop="ipSource">
      <el-input v-model="dataForm.ipSource" placeholder="ip来源"></el-input>
    </el-form-item>
    <el-form-item label="请求方法" prop="method">
      <el-input v-model="dataForm.method" placeholder="请求方法"></el-input>
    </el-form-item>
    <el-form-item label="方法描述" prop="operation">
      <el-input v-model="dataForm.operation" placeholder="方法描述"></el-input>
    </el-form-item>
    <el-form-item label="请求参数" prop="params">
      <el-input v-model="dataForm.params" placeholder="请求参数"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          exceptionJson: '',
          exceptionMessage: '',
          status: '',
          createTime: '',
          updateTime: '',
          ip: '',
          ipSource: '',
          method: '',
          operation: '',
          params: ''
        },
        dataRule: {
          exceptionJson: [
            { required: true, message: '异常对象json格式不能为空', trigger: 'blur' }
          ],
          exceptionMessage: [
            { required: true, message: '异常信息不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          ip: [
            { required: true, message: 'ip地址不能为空', trigger: 'blur' }
          ],
          ipSource: [
            { required: true, message: 'ip来源不能为空', trigger: 'blur' }
          ],
          method: [
            { required: true, message: '请求方法不能为空', trigger: 'blur' }
          ],
          operation: [
            { required: true, message: '方法描述不能为空', trigger: 'blur' }
          ],
          params: [
            { required: true, message: '请求参数不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/user/exceptionlog/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.exceptionJson = data.exceptionLog.exceptionJson
                this.dataForm.exceptionMessage = data.exceptionLog.exceptionMessage
                this.dataForm.status = data.exceptionLog.status
                this.dataForm.createTime = data.exceptionLog.createTime
                this.dataForm.updateTime = data.exceptionLog.updateTime
                this.dataForm.ip = data.exceptionLog.ip
                this.dataForm.ipSource = data.exceptionLog.ipSource
                this.dataForm.method = data.exceptionLog.method
                this.dataForm.operation = data.exceptionLog.operation
                this.dataForm.params = data.exceptionLog.params
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/exceptionlog/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'exceptionJson': this.dataForm.exceptionJson,
                'exceptionMessage': this.dataForm.exceptionMessage,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'ip': this.dataForm.ip,
                'ipSource': this.dataForm.ipSource,
                'method': this.dataForm.method,
                'operation': this.dataForm.operation,
                'params': this.dataForm.params
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
